package ua.kpi.oblamskyiv.counter;

import ua.kpi.oblamskyiv.testcase.Answers;
import ua.kpi.oblamskyiv.testcase.question.Question;

public class RangeCounter implements AnswersScoreCounter {
    private static final double MAX_SCORE = 1.0;
    private static final double MIN_SCORE = 0.0;

    private double maxDeviation;

    public RangeCounter(double maxDeviation) {
        this.maxDeviation = maxDeviation;
    }

    @Override
    public double countScore(Question question, Answers answers) {
        final int ANSWER_INDEX = 0;

        if (answers.size() != 1) {
            throw new IllegalArgumentException("Must be exactly 1 number!");
        }
        String userStringAnswer = answers.getAnswerText(ANSWER_INDEX);
        String correctStringAnswer = question.getCorrectAnswers().getAnswerText(ANSWER_INDEX);

        double userAnswer = Double.parseDouble(userStringAnswer);
        double correctAnswer = Double.parseDouble(correctStringAnswer);

        return (Math.abs(userAnswer - correctAnswer) <= maxDeviation) ? MAX_SCORE
                                                                      : MIN_SCORE;
    }
}
